/*
  Name: DeadProxyDatReader.cpp
  Copyright:
  Author: Carl Chatfield
  Date: 17/01/08 11:56
  Description: Reads tibia.dat file
*/

#include "DeadProxyDatReader.h"
#include "DeadProxyApp.h"

DatReader::DatReader(wxString * filename)
{
    FileName = filename;
    ThingInfo = new ThingData[7821];
    memset(ThingInfo, 0, sizeof(ThingData)*7821);
}

void DatReader::LoadDatFile()
{
    FILE * fp;
    fp = fopen(FileName->c_str(), wxT("rb"));
    if(fp == 0)
    {
        wxMessageBox(wxT("LAME"));
        return;
    }
    //wxString * output;
    fseek(fp, 12, SEEK_SET);
    unsigned char opt = 0xFF;
    unsigned short opt2 = 0x00;
    int itemPos = 0;
    wxString out;
    while(itemPos <= 7820)
    {
        opt = 0;
        ThingInfo[itemPos].ThingFlags = 0;
        while(opt != 0xFF)
        {
            //output = new wxString;
            //output->Printf("%i: ", itemPos + 100);
            fread(&opt, 1, 1, fp);
            //out.Printf("%i", opt);
            //wxMessageBox(out);
            switch(opt)
            {
                case 0x00:
                    ThingInfo[itemPos].ThingFlags |= WalkSpeed;
                    fread(&ThingInfo[0].Speed, 1, 2, fp);
                    //output->Append(wxT("Has Speed "));
                    break;
                case 0x01:
                    ThingInfo[itemPos].ThingFlags |= TopOrder1;
                    //output->Append(wxT("Top1 "));
                    break;
                case 0x02:
                    ThingInfo[itemPos].ThingFlags |= TopOrder2;
                    //output->Append(wxT("Top2 "));
                    break;
                case 0x03:
                    ThingInfo[itemPos].ThingFlags |= TopOrder3;
                    //output->Append(wxT("Top3 "));
                    break;
                case 0x04:
                    ThingInfo[itemPos].ThingFlags |= IsContainer;
                    //output->Append(wxT("Ctnr "));
                    break;
                case 0x05:
                    ThingInfo[itemPos].ThingFlags |= IsStackable;
                    //output->Append(wxT("Stck "));
                    break;
                case 0x06:
                    ThingInfo[itemPos].ThingFlags |= IsCorpse;
                    //output->Append(wxT("Crps "));
                    break;
                case 0x07:
                    //output->Append(wxT("Use "));
                    ThingInfo[itemPos].ThingFlags |= IsUsable;
                    break;
                case 0x08:
                    ThingInfo[itemPos].ThingFlags |= IsRune;
                    //output->Append(wxT("Rune "));
                    break;
                case 0x09:
                    ThingInfo[itemPos].ThingFlags |= IsWritable;
                    //output->Append(wxT("Wrt "));
                    fread(&ThingInfo[itemPos].WriteLimit, 1, 2, fp);
                    break;
                case 0x0A:
                    ThingInfo[itemPos].ThingFlags |= IsReadable;
                    //output->Append(wxT("Read "));
                    fread(&ThingInfo[itemPos].ReadLimit, 1, 2, fp);
                    break;
                case 0x0B:
                    ThingInfo[itemPos].ThingFlags |= IsFluidContainer;
                    //output->Append(wxT("Fld "));
                    break;
                case 0x0C:
                    ThingInfo[itemPos].ThingFlags |= IsSplash;
                    //output->Append(wxT("Spl "));
                    break;
                case 0x0D:
                    ThingInfo[itemPos].ThingFlags |= IsBlocking;
                    //output->Append(wxT("Blk "));
                    break;
                case 0x0E:
                    ThingInfo[itemPos].ThingFlags |= IsImmobile;
                    //output->Append(wxT("Imm "));
                    break;
                case 0x0F:
                    ThingInfo[itemPos].ThingFlags |= BlocksMissiles;
                    //output->Append(wxT("BlkM "));
                    break;
                case 0x10:
                    ThingInfo[itemPos].ThingFlags |= BlocksPath;
                    //output->Append(wxT("BlkP "));
                    break;
                case 0x11:
                    ThingInfo[itemPos].ThingFlags |= IsPickupable;
                    //output->Append(wxT("Pck "));
                    break;
                case 0x12:
                    ThingInfo[itemPos].ThingFlags |= IsHangable;
                    //output->Append(wxT("Hng "));
                    break;
                case 0x13:
                    ThingInfo[itemPos].ThingFlags |= IsHangableHorizontal;
                    //output->Append(wxT("Hoz "));
                    break;
                case 0x14:
                    ThingInfo[itemPos].ThingFlags |= IsHangableVertical;
                    //output->Append(wxT("Vrt "));
                    break;
                case 0x15:
                    ThingInfo[itemPos].ThingFlags |= IsRotatable;
                    //output->Append(wxT("Rot "));
                    break;
                case 0x16:
                    ThingInfo[itemPos].ThingFlags |= IsLightSource;
                    //output->Append(wxT("Light "));
                    fread(&ThingInfo[itemPos].LightColor, 1, 2, fp);
                    fread(&ThingInfo[itemPos].LightIntensity, 1, 2, fp);
                    break;
                case 0x18:
                    ThingInfo[itemPos].ThingFlags |= FloorChange;
                    //output->Append(wxT("FlrC "));
                    break;
                case 0x19:
                    ThingInfo[itemPos].ThingFlags |= IsOffset;
                    //output->Append(wxT("Off "));
                    fread(&ThingInfo[itemPos].OffsetX, 1, 2, fp);
                    fread(&ThingInfo[itemPos].OffsetY, 1, 2, fp);
                    break;
                case 0x1A:
                    ThingInfo[itemPos].ThingFlags |= IsRaised;
                    //output->Append(wxT("Rais "));
                    fread(&ThingInfo[itemPos].HeightChange, 1, 2, fp);
                    break;
                case 0x1B:
                    ThingInfo[itemPos].ThingFlags |= IsLayer;
                    //output->Append(wxT("Lyr "));
                    break;
                case 0x1C:
                    ThingInfo[itemPos].ThingFlags |= IsIdleAnimation;
                    //output->Append(wxT("IdAm "));
                    break;
                case 0x1D:
                    ThingInfo[itemPos].ThingFlags |= HasMiniMapColor;
                    //output->Append(wxT("MMap "));
                    fread(&ThingInfo[itemPos].MiniMapColor, 1, 2, fp);
                    break;
                case 0x1E:
                    ThingInfo[itemPos].ThingFlags |= HasHelpLens;
                    //output->Append(wxT("Act "));
                    fread(&opt2, 1, 2, fp);
                    break;
                case 0x1F:
                    ThingInfo[itemPos].ThingFlags |= IsGround;
                    //output->Append(wxT("GrndI "));
                    break;
            }
        }

        fread(&ThingInfo[itemPos].Width, 1, 1, fp);
        fread(&ThingInfo[itemPos].Height, 1, 1, fp);
        if(ThingInfo[itemPos].Width > 1 || ThingInfo[itemPos].Height > 1)
        {
            fseek(fp, 1, SEEK_CUR);
        }
        fread(&ThingInfo[itemPos].BlendFrames, 1, 1, fp);
        fread(&ThingInfo[itemPos].XDiv, 1, 1, fp);
        fread(&ThingInfo[itemPos].YDiv, 1, 1, fp);
        fread(&ThingInfo[itemPos].Rare, 1, 1, fp);
        fread(&ThingInfo[itemPos].Animations, 1, 1, fp);
        ThingInfo[itemPos].SpriteCount = ThingInfo[itemPos].Width *
                                         ThingInfo[itemPos].Height *
                                         ThingInfo[itemPos].BlendFrames *
                                         ThingInfo[itemPos].XDiv * 
                                         ThingInfo[itemPos].YDiv *
                                         ThingInfo[itemPos].Animations *
                                         ThingInfo[itemPos].Rare;
        ThingInfo[itemPos].Sprites = new unsigned short[ThingInfo[itemPos].SpriteCount];
        fread(ThingInfo[itemPos].Sprites, 2, ThingInfo[itemPos].SpriteCount, fp);
        //out.Printf("%i", ThingInfo[itemPos].Sprites[0]);
        //output->Append(out);
        //app->LogMessage(output);
        itemPos++;
    }
    fclose(fp);
}

        
    
